﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OreSystem.BLL;
using OreSystem.Entities;
namespace MainFrom {
    public partial class UserForm : Form {
        private UserBLL userBLL = new UserBLL();
        public UserForm() {
            InitializeComponent();
        }

        private void btn_search_Click(object sender, EventArgs e) {
            string keys = txt_Name.Text;
            dataGridView1.DataSource = userBLL.GetListByKeys(keys);
        }

        private void toolStripButton1_Click(object sender, EventArgs e) {
            UserEditFrom userEditFrom = new UserEditFrom();
            userEditFrom.NewUser = new User();
            if (userEditFrom.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                dataGridView1.DataSource = userBLL.GetAllList();
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e) {
            if (dataGridView1.SelectedRows.Count <= 0) {
                MessageBox.Show("没有选择要编辑的数据行！");
                return;
            }            
            int id = (int)dataGridView1.CurrentRow.Cells[0].Value;
            User user = userBLL.GetUserById(id);
            UserEditFrom userEditForm = new UserEditFrom();
            userEditForm.NewUser = user;
            if (userEditForm.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                dataGridView1.DataSource = userBLL.GetAllList();
            }
        }

        private void UserForm_Load(object sender, EventArgs e) {
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.Columns.Add("ID", "序号");
            dataGridView1.Columns.Add("UserName", "用户名");
            dataGridView1.Columns.Add("Role", "权限");           
            dataGridView1.Columns.Add("CreateDate", "添加时间");
            dataGridView1.Columns[0].DataPropertyName = "ID";
            dataGridView1.Columns[1].DataPropertyName = "UserName";
            dataGridView1.Columns[2].DataPropertyName = "Role";         
            dataGridView1.Columns[3].DataPropertyName = "CreateDate";


            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.MultiSelect = false;
            dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
            dataGridView1.DataSource = userBLL.GetAllList();
        }

        private void toolStripButton3_Click(object sender, EventArgs e) {
            if (dataGridView1.SelectedRows.Count <= 0) {
                MessageBox.Show("没有选择要删除的数据！");
                return;
            }
            int id = (int)dataGridView1.CurrentRow.Cells[0].Value;
            if (MessageBox.Show("你确定要删除吗？","信息提示",MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No) {
                return;
            }
            try {
                userBLL.Delete(userBLL.GetUserById(id));
                MessageBox.Show("删除成功！");
                dataGridView1.DataSource = userBLL.GetAllList();
            }
            catch (Exception ex) {

                MessageBox.Show("程序出错请联系管理员，错误提示" + ex.Message.ToString()); 
            }
            
        }
    }
}
